
using Microsoft.AspNetCore.Mvc;
using Infrastructure.Filters;
using ZR.Infrastructure;
using ZR.Service.BaseFrame.IService;
using ZR.Model.BaseFrame.Dto;
using ZR.Model.BaseFrame.Entity;
using ZR.Model;
using ZR.Admin.WebApi.MainModules;
using ZR.ServiceCore.Attributes;
using ZR.Model.Enums;
using ZR.Model.System.Vo;

namespace ZR.Admin.WebApi.Controllers.BaseFrame
{
    /// <summary>
    /// 模块
    /// </summary>
    [Route("baseFrame/module")]
    [ApiController]
    [EnableRspContract]
    [ModuleInfo(typeof(BaseFrameModule),"模块管理")]
    public class ModuleController : ModuleControllerBase
    {

            public ModuleController(IModuleService moduleService, MapsterAdaptEntityCfg mapsterAdaptEntityCfg)
                :base(moduleService, mapsterAdaptEntityCfg)
            {

            }

            protected override async Task CheckFormParam(object paramObj)
            {
                var req = paramObj as ModuleFormDto;
                MenuTypeEnum menuType;
                var isOk = Enum.TryParse<MenuTypeEnum>(req.MenuTypeCode, true, out menuType);
                isOk = isOk && Enum.IsDefined<MenuTypeEnum>(menuType);
                if (!isOk)
                {
                    throw new CustomException(ResultCode.PARAM_ERROR, "菜单类型无效");
                }
                switch (menuType)
                {
                    case MenuTypeEnum.Catalog:
                        req.PagePath = "";
                        req.LinkPath = "";
                        break;
                    case MenuTypeEnum.Page:
                        if (string.IsNullOrWhiteSpace(req.PagePath))
                        {
                            throw new CustomException(ResultCode.PARAM_ERROR, "菜单类型为页面时，页面路径不能为空");
                        }
                        req.LinkPath = "";
                        break;
                    case MenuTypeEnum.OuterLink:
                        if (string.IsNullOrWhiteSpace(req.LinkPath))
                        {
                            throw new CustomException(ResultCode.PARAM_ERROR, "菜单类型为外链时，链接路径不能为空");
                        }
                        req.PagePath = "";
                        break;
                }
                return;
            }

            [HttpGet("GetRoutersV2")]
            public async Task<List<RouterVo>> GetRoutersV2(long userId)
            {
                var list = await this._ModuleService.GetMenuTreeByUserId(userId);
                var result = this._ModuleService.BuildMenus(list);
                return result;
            }
    }
}